<?php
namespace App\Console\Commands\Task;

use App\Repositories\Task\TaskRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Services\InnerApi;

use App\Repositories\Task\TaskSubmitRepository;

/**
 * 不通过提交记录是否已经超时申诉期限
 * Class TaskOver
 * @package App\Console\Commands\Task
 * @version v1.3
 */
class TaskUnpassRejectTimeout extends Command
{
	protected $signature = 'task:unpassRejectTimeout';

	protected $description = 'check unpass submit record over rejection deadline';

	private $logger;

	private $taskSubmitRepository;
	private $taskRepository;

	public function __construct(TaskSubmitRepository $taskSubmitRepository, TaskRepository $taskRepository)
	{
		parent::__construct();
		$this->logger = Log::channel('console');
		$this->taskSubmitRepository = $taskSubmitRepository;
		$this->taskRepository = $taskRepository;
	}

	public function handle() {
		foreach ($this->taskSubmitRepository->getUnpassTimeout() as $task_id) {
			try {
				// 不通过记录超过申诉期限则给任务剩余可报名数+1
				$this->taskRepository->incQuotaRemain($task_id);
			} catch (\Exception $e) {
				$this->logger->error($this->signature, [$e->getMessage(), $task_id]);
				$this->error($e->getMessage());
			}
		}

		$this->info($this->signature . ' success！'. (new Carbon())->toDateTimeString());
	}
}